﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Tank.Resources;

namespace Tank.Role
{
    public class NPC
    {
        private Tank.Data.Direction indexDirection;
        private bool idnexStop = false;
        private Tank.Data.NPCType indexType;
        public Point indexPoint;
        public Bullet indexBullet = null;
        private bool isSend = false;//是否可以发射子弹
        private int ctime = 0;//距离上次发射时间
        private int speed;
        /// <summary>
        /// 当前运动方向
        /// </summary>
        public Tank.Data.Direction IndexDirection
        {
            get { return indexDirection; }
            set { indexDirection = value; }
        }

        /// <summary>
        /// 获取当前npc样式
        /// </summary>
        public Tank.Data.NPCType IndexType
        {
            get { return indexType; }
        }
        /// <summary>
        /// 当前坦克是否停止 true停止
        /// </summary>
        public bool IdnexStop
        {
            get { return idnexStop; }
            set { idnexStop = value; }
        }

        /// <summary>
        /// npc类
        /// </summary>
        /// <param name="NPCType">npc样式</param>
        /// <param name="p">npc坐标</param>
        public NPC(Tank.Data.NPCType NPCType, Point p)
        {
            this.indexType = NPCType;
            this.indexPoint = p;
            switch (NPCType)
            {
                case Data.NPCType.one:
                    this.speed = 3;
                    break;
                case Data.NPCType.two:
                    this.speed = 4;
                    break;
                case Data.NPCType.three:
                    this.speed = 2;
                    break;
            }
        }

        /// <summary>
        /// 获取NPC坦克图标
        /// </summary>
        /// <returns></returns>
        public Image getImage()
        {
            switch (indexDirection)
            {
                case Data.Direction.up:
                    if (indexType == Data.NPCType.one)
                        return ImageResources.NPCOneUp;
                    else if (indexType == Data.NPCType.two)
                        return ImageResources.NPCTwoUp;
                    else
                        return ImageResources.NPCThreeUp;
                case Data.Direction.down:
                    if (indexType == Data.NPCType.one)
                        return ImageResources.NPCOneDown;
                    else if (indexType == Data.NPCType.two)
                        return ImageResources.NPCTwoDown;
                    else
                        return ImageResources.NPCThreeDown;
                case Data.Direction.left:
                    if (indexType == Data.NPCType.one)
                        return ImageResources.NPCOneLeft;
                    else if (indexType == Data.NPCType.two)
                        return ImageResources.NPCTwoLeft;
                    else
                        return ImageResources.NPCThreeLeft;
                case Data.Direction.right:
                    if (indexType == Data.NPCType.one)
                        return ImageResources.NPCOneRight;
                    else if (indexType == Data.NPCType.two)
                        return ImageResources.NPCTwoRight;
                    else
                        return ImageResources.NPCThreeRight;
                default:
                    if (indexType == Data.NPCType.one)
                        return ImageResources.NPCOneDown;
                    else if (indexType == Data.NPCType.two)
                        return ImageResources.NPCTwoDown;
                    else
                        return ImageResources.NPCThreeDown;
            }
        }

        /// <summary>
        /// 获取NPC坦克坐标
        /// </summary>
        /// <returns></returns>
        public Point getPoint()
        {
            if (Data.NPCMove && idnexStop == false)
            {
                bool isCollision;
                indexPoint = new TankCollision().getTankPoint(indexDirection, indexPoint, out isCollision, true, speed);
                if (isCollision)
                {
                    Random r = new Random();
                    indexDirection = (Tank.Data.Direction)r.Next(0, 4);
                }
                //Data.NPCMove = false;
                SendBullet();
            }
            return indexPoint;
        }


        /// <summary>
        /// 发射子弹
        /// </summary>
        public void SendBullet()
        {
            ctime++;
            if (ctime > Data.NPcSendBulleTime)
            {
                isSend = true;                
            }
            if (isSend && (indexBullet == null || !Data.BulletList.Contains(indexBullet)))
            {//判断子弹是否为空或不存在 且可以发射子弹状态
                Point p = new Point();//子弹初始坐标
                switch (indexDirection)
                {
                    case Data.Direction.up:
                        p.X = indexPoint.X + Convert.ToInt32(Data.GridSize * Data.TankEnlarge * 3 / 4 / 2);//子弹的长宽是坦克的4分之1
                        p.Y = indexPoint.Y;
                        break;
                    case Data.Direction.down:
                        p.X = indexPoint.X + Convert.ToInt32(Data.GridSize * Data.TankEnlarge * 3 / 4 / 2);
                        p.Y = indexPoint.Y + Convert.ToInt32(Data.GridSize * Data.TankEnlarge);
                        break;
                    case Data.Direction.left:
                        p.X = indexPoint.X;
                        p.Y = indexPoint.Y + Convert.ToInt32(Data.GridSize * Data.TankEnlarge * 3 / 4 / 2);
                        break;
                    case Data.Direction.right:
                        p.X = indexPoint.X + Convert.ToInt32(Data.GridSize * Data.TankEnlarge);
                        p.Y = indexPoint.Y + Convert.ToInt32(Data.GridSize * Data.TankEnlarge * 3 / 4 / 2);
                        break;
                    default:
                        break;
                }
                Bullet b = new Bullet(indexDirection, Data.BulletType.NpcBullet, p);
                indexBullet = b;
                Data.BulletList.Add(b);
                isSend = false;
                ctime = 0;
            }
        }
    }
}
